app.controller('DecoratePicUpload', function($element, $scope, $state, rest, user, tipService, $window, uploadConf, $rootScope) {
    var data = angular.copy($scope.ngDialogData, {});
    $scope.params = {
      landlordId: user.get('landlordId')
    };

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 10;    //最大图片数量
    $scope.imgNew= [
        {
            text:'合同图片集合',
            name: 'contractPics',
            value: [],
            picType: 11,
            hink: '至少两张'
        },
        {
            text:'路牌照片',
            name: 'roadPics',
            value: [],
            picType: 12,
            hink: '至少两张'
        },
        {
            text:'门牌照片',
            name: 'doorplatePics',
            value: [],
            hink: '至少两张'
        },
        {
            text:'半开门照片',
            name: 'doubleDoorPics',
            value: [],
            picType: 13,
            hink: '至少两张'
        },
        {
            text:'房产证照片',
            name: 'houseCardPics',
            value: [],
            picType: 14,
            hink: '至少两张'
        },
        {
            text:'小区门口照片',
            name: 'doorPhotoPics',
            value: [],
            picType: 15,
            hink: '至少两张'
        }
    ];   
       
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;

    //获取房源模板列表
    var param_3012 = {};
    param_3012.landlordId = $scope.params.landlordId;
    rest.post({
        methodId: '3012',
        data: param_3012,
        success: function(res) {
            console.log( "3012",res );
            if (res.code == 0) {
                $scope.picModel = { selected: {name: '请选择模板', value: true} };
                $scope.picModelList = res.res.data;
                //console.log($scope.picModelList);
                $scope.$apply();
            } else {

            }
            $scope.disable_save_btn = false;
        },
        error: function() {
            $scope.disable_save_btn = false;
        }
    });

    $scope.chooseModel = function(index,picModelList) {
        $scope.editPicFlag = true;

        $scope.imgNew[0].value = [];
        $scope.imgNew[1].value = [];
        $scope.imgNew[3].value = [];
        $scope.imgNew[4].value = [];
        $scope.imgNew[5].value = [];
        var picModelId = picModelList[index].id;
        // console.log( picModelId );

        var param_3013 = {};
        param_3013.id = picModelId;
        $rootScope.modelId = picModelId;

        rest.post({
            methodId: '3013',
            data: param_3013,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.res.data.content;
                    data = JSON.parse( data );
                    for (var i in data) {
                        if (data[i].PicType == 11) {
                            $scope.imgNew[0].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 12) {
                            $scope.imgNew[1].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 13) {
                            $scope.imgNew[3].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 14) {
                            $scope.imgNew[4].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 15) {
                            $scope.imgNew[5].value.push(data[i].PicPath);
                        } 
                    }
                }
                $scope.disable_save_btn = false;
                $scope.$apply();
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
        $scope.$apply();
    }

    // $scope.addModelPics = function () {
    //     $state.go('app.houseOwner.housePicsTemplet');
    //     $scope.closeThisDialog();
    // }

    // $scope.editModelPics = function(templetId){
    //     $window.localStorage.setItem('housePicId',JSON.stringify({id:$rootScope.modelId}));
    //     $state.go('app.houseOwner.editHousePicsTemplet',{id: $rootScope.modelId});
    //     // console.log($rootScope.modelId);
    //     $scope.closeThisDialog();
    // }

    $scope.$on('contractDate.start', function(event, data) {
      $scope.params.contractStartDate = data + " 00:00:00";
    })
    $scope.$on('contractDate.end', function(event, data) {
      $scope.params.contractEndDate = data + " 00:00:00";
    })


    $scope.params.landlordId = user.get('landlordId');
    $scope.params.houseId = data.houseId;
    console.log(data.houseId);
     $scope.$watch('params.expectAmount', function(newValue, oldValue) {               
           if (newValue < 1){
                  $scope.params.expectAmount =1;
          }
      });
    $scope.makeSureConfirm = function() {

      //照片上传
      $scope.params.contractPics = [];
      $scope.params.roadPics = [];
      $scope.params.doorplatePics = [];
      $scope.params.doubleDoorPics = [];
      $scope.params.houseCardPics = [];
      $scope.params.doorPhotoPics = [];

      for (var i in uploadConf.imgNew[0].value) {
          $scope.params.contractPics.push(uploadConf.imgNew[0].value[i]);
      }
      for (var i in uploadConf.imgNew[1].value) {
              $scope.params.roadPics.push(uploadConf.imgNew[1].value[i]);
      }
      for (var i in uploadConf.imgNew[2].value) {
              $scope.params.doorplatePics.push(uploadConf.imgNew[2].value[i]);
      }
      for (var i in uploadConf.imgNew[3].value) {
              $scope.params.doubleDoorPics.push(uploadConf.imgNew[3].value[i]);
      }
      for (var i in uploadConf.imgNew[4].value) {
              $scope.params.houseCardPics.push(uploadConf.imgNew[4].value[i]);
      }
      for (var i in uploadConf.imgNew[5].value) {
              $scope.params.doorPhotoPics.push(uploadConf.imgNew[5].value[i]);
      }
      if ($scope.params.expectAmount <= 0 || $scope.params.expectAmount == undefined) {
        tipService.show("body", {
            message: "装修额度申请不能小于0",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.contractPics.length<=1) {
        tipService.show("body", {
            message: "合同图片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.roadPics.length<=1) {
        tipService.show("body", {
            message: "路牌照片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doorplatePics.length<=1) {
        tipService.show("body", {
            message: "门牌照片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doubleDoorPics.length<=1) {
        tipService.show("body", {
            message: "半开门照片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.houseCardPics.length<=1) {
        tipService.show("body", {
            message: "房产证照片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.params.doorPhotoPics.length<=1) {
        tipService.show("body", {
            message: "小区门口照片至少上传两张",
            notice:"danger"
        }, 3);
        return false;
      }
      if ($scope.contractStartDate > $scope.contractEndDate) {
            tipService.show("body", {
                message: "结束日期不能早于起始日期",
                notice:"danger"
            }, 3);
            return;
      }
      // console.log($scope.params);
      rest.post({
          methodId: '4009',
          data: $scope.params,
          success: function(res) {
              if (res.code == 0) {
                tipService.show("body", {
                    message: "装修评估提交成功"
                }, 3);
              }else{
                tipService.show("body", {
                    message: res.res.msg,
                    notice:"danger"
                }, 3);
              }
              $scope.confirm({});
              // $state.go('app.houseOwner.list');
          },
          error: function() {}
      })
    }
});


app.controller('ContractTimeCtrl',function($scope){
  $scope.today = function() {
      $scope.contractStartDate = moment().format('YYYY/MM/DD');
      $scope.contractEndDate = moment().add(2,'years').subtract(1, 'days').format('YYYY/MM/DD');
    };

    $scope.closeAll = function() {
      $scope.openContractBeginDated =
      $scope.openContractEndDated = false;
    }

    $scope.openContractBeginDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractBeginDated = true;
    };
    $scope.openContractEndDate = function($event) {
      $event.preventDefault();$event.stopPropagation();$scope.closeAll();
      $scope.openContractEndDated = true;
    };

    $scope.dateOptions = {
      format: 'yyyy/MM/dd',
      startingDay: 1,
      class: 'datepicker'
    };

    $scope.format = 'yyyy/MM/dd';

    $scope.today();

    $scope.$watch('contractStartDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.start',moment(newValue).format('YYYY/MM/DD'))
      }
    });
    $scope.$watch('contractEndDate',function(newValue,oldValue){
      if("[object Date]" === Object.prototype.toString.call(newValue)) {
         $scope.$emit('contractDate.end',moment(newValue).format('YYYY/MM/DD'))
      }
    });
});
